/*
* @Author: majingyu
* @Date:   2017-05-25 21:38:25
* @Last Modified by:   jingyu.ma
* @Last Modified time: 2017-08-12 18:31:25
*/
import Fetch from 'common/fetch';

//查找周边所有的服务
const getUserSevices = (type, data) => {
    let action = {
        type:type
    }
    if(type == 'GET_USER_SUCCESS'){
        action['data'] = data;
    }else if(type == 'GET_USER_ERROR'){
        action['e'] = data;
    }
    return action;
}

const GET_USER_BIGIN = 'GET_USER_BIGIN';
const GET_USER_SUCCESS = 'GET_USER_SUCCESS';
const GET_USER_ERROR = 'GET_USER_ERROR';

export const getUserMessage = () => (dispatch, getState) => {
    dispatch(getUserSevices(GET_USER_BIGIN));
    let myHeaders = new Headers();
    myHeaders.append('Content-Type', 'application/json');
    return Fetch('/web/login/user',{
        method:'POST',
        // body:JSON.stringify(params),
        credentials:'include',
        headers:myHeaders
    }).then(res=>res.json()).then((data)=>{
        if(data.status == 200){
            dispatch(getUserSevices(GET_USER_SUCCESS,data.data));
        }else{
            message.error(data.err)
            dispatch(getUserSevices(GET_USER_ERROR,data));
        }
    }).catch(e=>{
        dispatch(getUserSevices(GET_USER_ERROR,e));
    })
}

const submitEvaluateType = (type, data) => {
    let action = {
        type:type
    }
    if(type == SUBMIT_EVALUATE_SUCCESS){
        action['data'] = data;
    }else if(type == SUBMIT_EVALUATE_ERROR){
        action['e'] = data;
    }else if(type == SUBMIT_EVALUATE_BIGIN){
        action['data'] = data;
    }
    return action;
}
const SUBMIT_EVALUATE_BIGIN = 'SUBMIT_EVALUATE_BIGIN';
const SUBMIT_EVALUATE_SUCCESS = 'SUBMIT_EVALUATE_SUCCESS';
const SUBMIT_EVALUATE_ERROR = 'SUBMIT_EVALUATE_ERROR';

export const submitEvaluate = (params) => (dispatch, getState) => {
    dispatch(submitEvaluateType(SUBMIT_EVALUATE_BIGIN, '正在提交...'));
    let myHeaders = new Headers();
    myHeaders.append('Content-Type', 'application/json');
    return Fetch('/wx/order/rank',{
        method:'POST',
         body:JSON.stringify(params),
        credentials:'include',
        headers:myHeaders
    }).then(res=>res.json()).then((data)=>{
        if(data.status == 200){
            dispatch(submitEvaluateType(SUBMIT_EVALUATE_SUCCESS,'提交意见'));
        }else{
            message.error(data.err)
            dispatch(submitEvaluateType(SUBMIT_EVALUATE_ERROR,'提交意见'));
        }
    }).catch(e=>{
        dispatch(submitEvaluateType(SUBMIT_EVALUATE_ERROR,'提交意见'));
    })
}